﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Dimok.Web.UI;

public partial class Controls_Admin_Collection_Buckets : System.Web.UI.UserControl,IScriptControl {
	const string CLASS_NOT_IN_BUCKET = "notInBucket";
	const string CLASS_IN_BUCKET = "inBucket";
	ScriptManager sm;
	Data.LINQ.MotoBank db;
	List<Data.LINQ.CollectionPerformanceResult> collPerf;
	List<Data.LINQ.v_ADS_Collector> collectors;
	List<Data.LINQ.v_CollectToday> collectToday;
	protected void Page_Load(object sender, EventArgs e) {
		db = new Data.LINQ.MotoBank();
		db.CommandTimeout = 60;
		collPerf = db.CollectionPerformance(1).ToList();
		collectors = db.v_ADS_Collectors.ToList();
		collectToday = db.v_CollectTodays.ToList();
		dsCollectors.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(dsCollectors_Selecting);
		dsCollectionContainers.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(dsCollectionContainers_Selecting);
	}

	void dsCollectionContainers_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
		e.Result = from ct in collectToday
							 group ct by ct.CompanyCode into compSpeed
							 select new {
								 CompanyCode = compSpeed.Key,
								 Accounts = compSpeed.Sum(ct => ct.Accounts),
								 LoansPerDay = compSpeed.Sum(ct => ct.Speed_LoansPerDay),
								 Ratio = Math.Round(Convert.ToDecimal(compSpeed.Sum(ct => 1.0 * ct.Accounts) / compSpeed.Sum(ct => ct.Speed_LoansPerDay)) * 100)
							 };

		//e.Result = collectToday.Select(s => new { Container = s.CompanyCode }).Distinct();
	}

	protected void dsCollectionBuckets_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
		e.Result = from c in collectToday
							 where c.CompanyCode == (e.WhereParameters["Container"] + "")
							 orderby c.GroupName
							 select new {
								 SID = c.SID_Group,
								 c.GroupName,
								 c.Accounts,
								 LoansPerDay = c.Speed_LoansPerDay,
								 Ratio = string.Format("{0:p0}", Convert.ToDecimal(c.Accounts) / c.Speed_LoansPerDay)
							 };
	}

	protected void dsCollectorsByBucket_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
		var coll = (from c in collectors
													where c.SID_Group == (e.WhereParameters["SID"] + "")
													select new {
														Login = c.sAMAccountName,
														DisplayName = c.DisplayName.ToLower()
													}
							 ).Distinct().ToList();
		e.Result = from c in coll
							 join cp in collPerf on c.Login equals cp.NoteUser into gj
							 from subCP in gj.DefaultIfEmpty()
							 orderby c.DisplayName
							 select new { c.Login, c.DisplayName, LoansPerDay = subCP == null ? 0 : Convert.ToInt16(Math.Round(subCP.LoansPerDay.Value,0)) };
	}

	protected void dsCollectors_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
		var inBucket = collectors.Where(s=>s.InBucket == true).Select(s=>s.sAMAccountName).Distinct();
		e.Result = (from c in collectors
								group c by new{Login = c.sAMAccountName,DisplayName = c.DisplayName.ToLower()} into g
								select new {
									Login = g.Key.Login,
									DisplayName = g.Key.DisplayName,
									InBucket = g.Max(c => Convert.ToByte(c.InBucket)),
									Class = g.Max(c => Convert.ToByte(c.InBucket)) == 1 ? CLASS_IN_BUCKET : CLASS_NOT_IN_BUCKET
								}
							 ).Distinct().OrderBy(s => s.InBucket).ThenBy(s => s.DisplayName); ;
	}

	#region Ajax registration
	protected override void OnPreRender(EventArgs e) {
		if (!this.DesignMode) {
			sm = ScriptManager.GetCurrent(Page);
			if (sm == null) throw new HttpException("A ScriptManager control must exist on the current page.");
			sm.RegisterScriptControl(this);
		}
		element.AppendStyle(Attributes);
		HtmlLink cssLink = new HtmlLink();
		cssLink.Href = System.IO.Path.ChangeExtension(AppRelativeVirtualPath, "css");
		cssLink.Attributes.Add("type", "text/css");
		cssLink.Attributes.Add("rel", "stylesheet");
		Page.Header.Controls.Add(cssLink);
		base.OnPreRender(e);
	}
	protected override void Render(HtmlTextWriter writer) {
		if (!this.DesignMode) sm.RegisterScriptDescriptors(this);
		base.Render(writer);
	}
	#endregion

	#region IScriptControl Members

	public IEnumerable<ScriptDescriptor> GetScriptDescriptors() {
		ScriptControlDescriptor descriptor = new ScriptControlDescriptor("motobankcorp.Buckets", element.ClientID);
		descriptor.AddProperty("classInBucket", CLASS_IN_BUCKET);
		descriptor.AddProperty("classNotInBucket", CLASS_NOT_IN_BUCKET);
		yield return descriptor;
	}

	public IEnumerable<ScriptReference> GetScriptReferences() {
		yield return new ScriptReference(System.IO.Path.ChangeExtension(AppRelativeVirtualPath, "js"));
	}

	#endregion

	#region IScriptControl Members

	IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors() {
		return GetScriptDescriptors();
	}

	IEnumerable<ScriptReference> IScriptControl.GetScriptReferences() {
		return GetScriptReferences();
	}

	#endregion
}
